//
//  Login.swift
//  心迹_01
//
//  Created by ibokan on 2017/7/9.
//  Copyright © 2017年 ibokan. All rights reserved.
//

import UIKit
import LeanCloud

class LoginViewController: UIViewController {
    
        
    
    // 创建背景图片
    let mainImageView = UIImageView(image:UIImage(named:"beijing_15"))
    // 图片加高斯模糊
    func blurImage(imageToBlur:UIImage) -> UIImage {
        let gaussianBlurFilter = CIFilter(name: "CIGaussianBlur")
        gaussianBlurFilter?.setValue(CIImage(cgImage: imageToBlur.cgImage!), forKey:kCIInputImageKey)
        gaussianBlurFilter?.setValue(2.0, forKey: "inputRadius")
        let inputImage = CIImage(cgImage: imageToBlur.cgImage!)
        
        let outputImage = gaussianBlurFilter?.outputImage
        let context = CIContext(options: nil)
        let cgimg = context.createCGImage(outputImage!, from: inputImage.extent)
        
        return UIImage(cgImage: cgimg!)
    }
    // 创建背景图片方法
    func MainImage() -> UIImage{
        
        mainImageView.frame = CGRect(x:0, y:0, width:view.frame.width, height:view.frame.height)
        
        // 将UIImageView转换成UIImage
        UIGraphicsBeginImageContextWithOptions(mainImageView.layer.bounds.size, mainImageView.layer.isOpaque, 0.0)
        mainImageView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let outputImg = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return self.blurImage(imageToBlur: outputImg!)
    }
    
    // 创建顶部logo
    let loginImage = UIImageView(image:UIImage(named:"touxiang_02"))
    
    // 创建顶部logo方法
    func Loginimage() {
        loginImage.frame = CGRect(x:127, y:64, width:120, height:120)
        
        // logo切圆角
        let imageLayer: CALayer = loginImage.layer
        imageLayer.masksToBounds = true
        imageLayer.cornerRadius = loginImage.frame.size.height / 2
        
        self.view.addSubview(loginImage)
    }
    
    // 创建账号密码输入框
    let username = UITextField(frame: CGRect(x:47, y:214, width:280, height:40))
    let password = UITextField(frame: CGRect(x:47, y:264, width:280, height:40))
    
    // 创建账号密码方法
    func LoginText() {
        
        // 设置边框样式为圆角矩形
        username.borderStyle = UITextBorderStyle.roundedRect
        password.borderStyle = UITextBorderStyle.roundedRect
        // 待选择文字
        username.placeholder = "请输入邮箱"
        password.placeholder = "请输入密码"
        // 字数超过后缩小字号
        username.adjustsFontSizeToFitWidth = true
        password.adjustsFontSizeToFitWidth = true
        //最小可缩小的字号
        username.minimumFontSize = 14
        password.minimumFontSize = 14
        // 清除按钮设置，编辑时出现清除按钮
        username.clearButtonMode = .whileEditing
        password.clearButtonMode = .whileEditing
        // 弹出键盘设置
        username.keyboardType = UIKeyboardType.emailAddress
        password.keyboardType = UIKeyboardType.alphabet
        // 键盘弹出
        username.becomeFirstResponder()
        password.becomeFirstResponder()
        // 键盘收起
        username.resignFirstResponder()
        password.resignFirstResponder()
        // return键设置
        username.returnKeyType = UIReturnKeyType.next
        password.returnKeyType = UIReturnKeyType.go
        // 给输入框添加图片
        username.leftView = UIImageView(image: UIImage(named: "username_03"))
        username.leftViewMode = UITextFieldViewMode.always
        password.leftView = UIImageView(image: UIImage(named: "password_03"))
        password.leftViewMode = UITextFieldViewMode.always
        
        
        self.view.addSubview(username)
        self.view.addSubview(password)
    }
    
    // 创建登录按钮
    let loginButton:UIButton = UIButton(type: .system)
    
    // 登录按钮方法
    func Loginbutton() {
        loginButton.frame = CGRect(x: 47, y: 314, width: 280, height: 40)
        loginButton.setTitle("登录", for: .normal)
        loginButton.setTitleColor(UIColor.white, for: .normal)
        loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        loginButton.backgroundColor = UIColor.init(red: 93/255, green: 172/255, blue: 250/255, alpha: 100)
//      loginButton.layer.borderWidth = 2
        loginButton.layer.cornerRadius = 6
        
        self.view.addSubview(loginButton)
    }
    
    // 创建忘记密码按钮
    let forgetPassword:UIButton = UIButton(type:.system)
    
//    // 跳转到登录
//    func InLogin(){
//        let inLogin = LoginViewController()
//        self.navigationController!.pushViewController(inLogin, animated: true)
//    }
    
    //创建忘记密码按钮方法
    func ForgetPassword() {
        //设置按钮位置和大小
        forgetPassword.frame = CGRect(x:227, y:374, width:100, height:30)
        //设置按钮文字
        forgetPassword.setTitle("忘记密码？", for:.normal)
        // 按钮点击事件
//        backLogin.addTarget(self, action:#selector(InLogin), for:.touchUpInside)
        self.view.addSubview(forgetPassword)
    }
    
    // 创建注册按钮
    let inRegister:UIButton = UIButton(type:.system)
    
        // 跳转到注册
        func InRegister_02(){
            let register = RegisterViewController()
            self.navigationController!.pushViewController(register, animated: true)
        }
    
    //创建注册按钮方法
    func InRegister() {
        //设置按钮位置和大小
        inRegister.frame = CGRect(x:47, y:374, width:100, height:30)
        //设置按钮文字
        inRegister.setTitle("注册账号", for:.normal)
        // 按钮点击事件
        inRegister.addTarget(self, action:#selector(InRegister_02), for:.touchUpInside)
        self.view.addSubview(inRegister)
    }
    
    // 创建QQ登录按钮
    let qqLogin:UIButton = UIButton(type:.system)
    // 创建微信按钮
    let weixinLogin:UIButton = UIButton(type:.system)
    // 创建新浪按钮
    let xlLogin:UIButton = UIButton(type:.system)
    
    
    //创建第三方按钮方法
    func DsfLogin() {
        
        // QQ登录
        //设置按钮位置和大小
        qqLogin.frame = CGRect(x:47, y:557, width:50, height:50)
        //设置按钮图片
        qqLogin.setBackgroundImage(UIImage(named:"QQ_01"), for:.normal)
        // 按钮点击事件
        // inRegister.addTarget(self, action:#selector(InRegister_02), for:.touchUpInside)
        self.view.addSubview(qqLogin)
        
        // 微信登录
        //设置按钮位置和大小
        weixinLogin.frame = CGRect(x:162, y:557, width:50, height:50)
        //设置按钮图片
        weixinLogin.setBackgroundImage(UIImage(named:"weixin_01"), for:.normal)
        // 按钮点击事件
        // inRegister.addTarget(self, action:#selector(InRegister_02), for:.touchUpInside)
        self.view.addSubview(weixinLogin)
        
        // 新浪登录
        //设置按钮位置和大小
        xlLogin.frame = CGRect(x:277, y:557, width:50, height:50)
        //设置按钮图片
        xlLogin.setBackgroundImage(UIImage(named:"xinlang_01"), for:.normal)
        // 按钮点击事件
        // inRegister.addTarget(self, action:#selector(InRegister_02), for:.touchUpInside)
        self.view.addSubview(xlLogin)
    }
    
    
    
    // 添加横线方法
    func LineImage() {
        // 添加横线属性
        let lineImage_01 = UIImageView(frame: CGRect(x: 27, y: 467, width: 90, height: 1))
        lineImage_01.backgroundColor = UIColor.init(red: 38/256, green: 38/256, blue: 38/256, alpha: 100)
        self.view.addSubview(lineImage_01)
        
        // 添加横线属性
        let lineImage_02 = UIImageView(frame: CGRect(x: 257, y: 467, width: 90, height: 1))
        lineImage_02.backgroundColor = UIColor.init(red: 38/256, green: 38/256, blue: 38/256, alpha: 100)
        self.view.addSubview(lineImage_02)
    }
    
    // 添加label方法
    func QTLabel() {
        // 添加label属性
        let qtLabel = UILabel(frame:CGRect(x:135, y:457, width:105, height:21))
        qtLabel.text = "其他登录方式"
        self.view.addSubview(qtLabel);
        
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 顶部logo方法
        self.Loginimage()
        
        // 添加文本框方法
        self.LoginText()
        
        // 登录按钮方法
        self.Loginbutton()
        
        // 忘记密码方法
        self.ForgetPassword()
        
        // 注册按钮方法
        self.InRegister()
        
        // 第三方登录方法
        self.DsfLogin()
        
        // 横线方法
        self.LineImage()
        
        // label方法
        self.QTLabel()
        
        // 添加背景图片
        self.view.backgroundColor = UIColor(patternImage: MainImage())
        
    }
    
    //内存警告时调用
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
